/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.web.controller.sys;

import com.cgmnx.admin.biz.log.OperationLogBiz;
import com.cgmnx.admin.common.bean.ApiRes;
import com.cgmnx.admin.common.bean.Limiter;
import com.cgmnx.admin.common.constants.WebConstant;
import com.cgmnx.admin.dao.condition.LogCondition;
import com.cgmnx.admin.dao.entity.rbac.OperationLogEntity;
import com.cgmnx.admin.web.converter.vm.OperationVmConverter;
import com.cgmnx.admin.web.model.vm.OperationLogVm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author: created  Date: 16/12/18 Time: 下午1:13
 * @version: 1.0
 */
@Slf4j
@Controller
public class LogController {

    private final static String TIME_SUFFIX = " 00:00:00";

    @Autowired
    private OperationLogBiz operationLogBiz;

    @Autowired
    private OperationVmConverter operationVmConverter;

    @ResponseBody
    @RequestMapping("/cgmnx/log/queryPage.json")
    public ApiRes queryPage(@RequestParam(value = "operationId", required = false) String operationId,
                            @RequestParam(value = "logType", required = false) Integer logType,
                            @RequestParam(value = "startTime", required = false) String startTime,
                            @RequestParam(value = "endTime", required = false) String endTime,
                            @RequestParam(value = "offset", required = false, defaultValue = WebConstant.DEFAULT_PAGE_OFFSET) Integer offset,
                            @RequestParam(value = "pageSize", required = false, defaultValue = WebConstant.DEFAULT_PAGE_SIZE) Integer pageSize) {

        // 参数转换
        LogCondition condition = buildCondition(operationId, logType, startTime, endTime);
        // 查询
        Integer count = operationLogBiz.count(condition);
        if (count <= 0) {
            return ApiRes.emptyList();
        }

        List<OperationLogEntity> operationLogEntities = operationLogBiz.queryPage(condition, Limiter.create(offset, pageSize));

        List<OperationLogVm> convert = operationVmConverter.convert(operationLogEntities);
        return ApiRes.list(convert, count);

    }

    private LogCondition buildCondition(String operationId, Integer logType, String startTime, String endTime) {

        LogCondition condition = new LogCondition();
        if (StringUtils.isNotBlank(operationId)) {
            condition.setOperationId(operationId);
        }

        if (logType != null) {
            condition.setType(logType);
        }

        if (StringUtils.isNotBlank(startTime)) {
            startTime = startTime + TIME_SUFFIX;
            condition.setStartTime(Timestamp.valueOf(startTime));
        }
        if (StringUtils.isNotBlank(endTime)) {
            endTime = endTime + TIME_SUFFIX;
            condition.setEndTime(Timestamp.valueOf(endTime));
        }
        return condition;
    }
}